package com.meij.http;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.meij.bean.Album;
import com.meij.bean.Avatar;
import com.meij.bean.Check;
import com.meij.bean.ErrorInfo;
import com.meij.bean.Person;
import com.meij.bean.UserInfo;
import com.util.DebugLog;

public class Json {
	/***
	 * 得到json返回的错误相关信息
	 * 
	 * @param object
	 * @param context
	 * @return
	 */
	public static ErrorInfo getErrorInfo(JSONObject object) {
		DebugLog.i("JSON", object.toString());
		Gson gson = new Gson();
		ErrorInfo error = gson.fromJson(object.toString(), ErrorInfo.class);
		switch (error.getCode()) {
		case 501:
			error.setErrormessage("用户权限验证失败");
			break;
		case 502:
			error.setErrormessage("未知的服务端错误");
			break;
		case 401:
			error.setErrormessage("用户或密码错误");
			break;
		case 402:
			error.setErrormessage("用户已经注册");
			break;
		case 403:
			error.setErrormessage("用户金币不足");
			break;
		case 404:
			error.setErrormessage("用户密码错误");
			break;
		case 601:
			error.setErrormessage("搏击宝贝不存在");
			break;
		case 701:
			error.setErrormessage("直播比赛不存在");
			break;
		case 801:
			error.setErrormessage("点播视频不存在");
			break;
		case 901:
			error.setErrormessage("竞彩记录不存在");
			break;
		}
		return error;
	}

	/***
	 * 得到用户的登录信息
	 * 
	 * @param object
	 * @return
	 */
	public static UserInfo getUserInfo(JSONObject object) {
		UserInfo userInfo = new UserInfo();
		Gson gson = new Gson();
		userInfo = gson.fromJson(object.toString(), UserInfo.class);
		return userInfo;
	}

	/***
	 * 得到用户的描述信息
	 */
	public static Person getPerson(JSONObject object) {
		Person person = new Person();
		Gson gson = new Gson();
		person = gson.fromJson(object.toString(), Person.class);
		return person;
	}

	/***
	 * 得到版本检测相关信息
	 * 
	 * @param object
	 * @return
	 */
	public static Check getCheck(JSONObject object) {
		Gson gson = new Gson();
		Check check = gson.fromJson(object.toString(), Check.class);
		return check;
	}

	/***
	 * 得到头像墙
	 */
	public static ArrayList<Avatar> getAvatars(JSONObject object) {
		Gson gson = new Gson();
		try {
			JSONArray jsonArray = object.getJSONArray("avatars");
			ArrayList<Avatar> avatars = gson.fromJson(jsonArray.toString(), new TypeToken<ArrayList<Avatar>>() {
			}.getType());
			return avatars;
		} catch (JSONException e) {
			e.printStackTrace();
			return new ArrayList<Avatar>();
		}
	}

	/***
	 * 得到相册
	 * 
	 * @param object
	 * @return
	 */
	public static ArrayList<Album> getAlbums(JSONObject object) {
		Gson gson = new Gson();
		try {
			JSONArray jsonArray = object.getJSONArray("album");
			ArrayList<Album> albums = gson.fromJson(jsonArray.toString(), new TypeToken<ArrayList<Album>>() {
			}.getType());
			return albums;
		} catch (JSONException e) {
			e.printStackTrace();
			return new ArrayList<Album>();
		}
	}

	/***
	 * 得到宝贝的详细信息
	 * 
	 * @param object
	 * @return
	 */
	public static Avatar getAvatar(JSONObject object) {
		Gson gson = new Gson();
		Avatar avatar = gson.fromJson(object.toString(), Avatar.class);
		return avatar;
	}

	public static String getToken(JSONObject object) {
		String token = "";
		try {
			token = object.getString("token");
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return token;
	}

	public static String getUserkey(JSONObject object) {
		String userkey = "";
		try {
			userkey = object.getString("userkey");
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return userkey;
	}

	/***
	 * 得到验证码
	 * 
	 * @param object
	 * @return
	 */
	public static String getVcode(JSONObject object) {
		try {
			String vcode = object.getString("vcode");
			return vcode;
		} catch (JSONException e) {
			e.printStackTrace();
			return "";
		}
	}

	/***
	 * 得到用户角色
	 * 
	 * @param object
	 * @return
	 */
	public static int getRoleApply(JSONObject object) {
		try {
			int vcode = object.getInt("roleApply");
			return vcode;
		} catch (JSONException e) {
			e.printStackTrace();
			return 0;
		}
	}

	/***
	 * 是否已经关注了当前用户
	 * 
	 * @param object
	 * @return
	 */
	public static boolean getIsFollowed(JSONObject object) {
		try {
			return object.getBoolean("isFollowed");
		} catch (JSONException e) {
			e.printStackTrace();
			return false;
		}
	}
}
